package com.jereh.music.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;

import com.jereh.music.PlayApplication;
import com.jereh.music.entity.Song;
import com.jereh.music.ible.IMusic;

import java.io.IOException;
import java.util.List;

/**
 * 播放音乐的服务
 */
public class PlayService extends Service {

    private MediaPlayer mp;
    private PlayApplication app;

    @Override
    public void onCreate() {
        super.onCreate();
        app=(PlayApplication)getApplication();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        play(0);
        PlayBinder playBinder=new PlayBinder();
//        app.music=playBinder;
        return playBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        if(mp!=null) {
            mp.stop();
            mp.release();
            mp=null;
        }
        return super.onUnbind(intent);
    }

    /**
     * 播放
     */
    public void play(int position) {
        if(app.songList.isEmpty()){
            return;
        }
        //获取数据之后进行播放
        mp=new MediaPlayer();
        //当前要播放的歌曲
        Song currentSong=app.songList.get(position);
        //解析出播放音乐的路径
        Uri uri=Uri.parse(currentSong.getBitrate().getFile_link());
        try {
            //设置数据源
            mp.setDataSource(this,uri);
            //准备
            mp.prepareAsync();
            //播放
            mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public class PlayBinder extends Binder implements IMusic{

        @Override
        public void initMusic(List<Song> list) {
            app.songList.clear();
            //添加所有的音乐
            app.songList.addAll(list);
            play(0);
        }

        @Override
        public void moveon() {
            if(mp!=null&&!mp.isPlaying()) {
                mp.start();
            }
        }

        @Override
        public void pause() {
            if(mp!=null&&mp.isPlaying()) {
                mp.pause();
            }
        }

        @Override
        public void next() {
            //数据源
            int position=app.songPosition;
            if(position==(app.songList.size()-1)) {
                app.songPosition=0;
            } else {
                app.songPosition++;
            }
            stopPlay();
            play(app.songPosition);
        }

        @Override
        public void prev() {
            //数据源
            int position=app.songPosition;
            if(position==0) {
                app.songPosition=app.songList.size()-1;
            } else {
                app.songPosition--;
            }
            stopPlay();
            play(app.songPosition);
        }
        private void stopPlay() {
            if(mp!=null) {
                mp.stop();
                mp.release();
                mp=null;
            }
        }
    }
}
